package cn.st4rlight.util.excel.converter;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import cn.st4rlight.util.constant.SymbolConstant;

/**
 * 用于easy-excel中 空字符串 --> -
 *
 * @author st4rlight <st4rlight@163.com>
 * Created on 2024-04-15
 */
public class Empty2HyphenConverter implements Converter<String> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
        String value = context.getValue();
        value = StringUtils.isNotBlank(value) ? value : SymbolConstant.HYPHEN;
        return new WriteCellData<>(CellDataTypeEnum.STRING, value);
    }
}
